﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.IO;
using System.Web.Http;
using ExpenseSystem.Core;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Core.Converter;
using ExpenseSystem.Fether;
using ExpenseSystem.Common;
using ExpenseSystem.Lor;
using ExpenseSystem.Model;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Core.SalesPointModules;

namespace ExpenseSystem.Controllers
{
    [RoutePrefix("api/SalesPoint")]

    public class SalesPointController : ApiController
    {
        SetInhibition setInhibition;
        GetInhibition getInhibition;
        SetInhibitionMaker inhibitionMaker = new StateInhibitionMaker();
        GetInhibitionMaker getInhibitionMaker = new GetInhibitionMaker();
        public SalesPointController()
        {
            setInhibition = inhibitionMaker.Make();
            getInhibition = getInhibitionMaker.Make();
        }

        SalesPointFetcher fetcher = new SalesPointFetcher();
        SalesPointEmployeeFetcher dfetcher = new SalesPointEmployeeFetcher();
        SalesPointManager manager = SalesPointManager.CreateInstance();
        SalesPointEmployeeManager dmanager = SalesPointEmployeeManager.CreateInstance();

        [Route("List")]
        [HttpGet]
        public ResponseModel List([FromUri]RequestArgs paramModels)
        {
            return NormalControllerHelper.List(fetcher, "BAS_SALESPOINT_LIST", paramModels, getInhibition);
        }
        [Route("Get")]
        [HttpGet]
        public ResponseModel Get(long id)
        {
            return NormalControllerHelper.Get(fetcher, "BAS_SALESPOINT_LIST", id, getInhibition);
        }

        [Route("Set")]
        [HttpPost]
        public ResponseModel Set(SalesPointModel data)
        {
            return NormalControllerHelper.Set(fetcher, "BAS_SALESPOINT_SET", data, manager, setInhibition, getInhibition);
        }

        [Route("Remove")]
        [HttpPost]
        public ResponseModel Remove(SalesPointModel data)
        {
            return NormalControllerHelper.Remove("BAS_SALESPOINT_DEL", data, manager);
        }
        [Route("RemoveWithDetail")]
        [HttpGet]
        public ResponseModel RemoveWithDetail([FromUri]long id)
        {
            return NormalControllerHelper.RemoveWithDetail(new SalesPointEmployeeFetcher(), "BAS_SALESPOINT_DEL", id, manager, dmanager);
        }

        [Route("RemoveById")]
        [HttpGet]
        public ResponseModel RemoveById([FromUri]long[] id)
        {
            return NormalControllerHelper.RemoveById("BAS_SALESPOINT_DEL", id, manager);
        }
        [Route("ListEmployees")]
        [HttpGet]
        public ResponseModel ListDetail(long spId)
        {
            return NormalControllerHelper.ListDetail(dfetcher, "BAS_PRODUCTION_LIST", spId, getInhibition);
        }

        [Route("SetEmployees")]
        [HttpPost]
        public ResponseModel SetDetail([FromBody] RequestModel<SalesPointEmployeeModel> requestData)
        {
            ResponseModel response = new ResponseModel();
            if (requestData.Data==null)
            {
                response.Result = Definition.RESP_SET_DATA_ERROR;
                response.Count = 0;
                response.Data = null;
                response.Message = "传入参数有异常";
                return response;
            }
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                if (requestData.Data[i].SalesPointId != requestData.MasterId)
                {
                    response.Result = Definition.RESP_SET_DATA_ERROR;
                    response.Count = 0;
                    response.Data = null;
                    response.Message = "传入参数有异常";
                    return response;
                }
            }
            return NormalControllerHelper.SetDetail(dfetcher, dfetcher, "BAS_PRODUCTION_SET", requestData, dmanager, setInhibition);
        }
    }
}
